package com.labor_management_system.Controller;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.labor_management_system.Entity.Project;
import com.labor_management_system.Entity.Team;
import com.labor_management_system.Service.EnterprisesService;
import com.labor_management_system.Service.ProjectService;
import com.labor_management_system.Service.TeamService;
import com.labor_management_system.Utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

@RestController
@CrossOrigin
public class LaborTeamsController { //劳务班组管理
    @Autowired
    TeamService teamService;
    @Autowired
    EnterprisesService enterprisesService;
    @Autowired
    ProjectService projectService;

    @PostMapping("/TEST/Gen")
    public R gen(){
        IntStream.range(0, 10).forEach(i -> teamService.getBaseMapper().insert(new Team(UUID.randomUUID().toString(), "Team " + i, "Contractor " + i, ThreadLocalRandom.current().nextInt(1, 6), "138" + String.format("%08d", ThreadLocalRandom.current().nextInt(100000000)), "Enterprise " + i, ThreadLocalRandom.current().nextInt(1, 11))));
        return R.ok();
    }


    @GetMapping ("/GET/PreCreateTeam")
    public R preCreateTeam(){
        //获取项目信息和公司信息,包装类，或者拆开来，写两个函数

        return R.ok().data("ProjectNameAndID",new JSONArray(projectService.getProjectIDAndName()))
                     .data("EnterprisesNameAndID",new JSONArray(enterprisesService.getEnterprisesIDAndName()));
    }
    @PostMapping("/POST/CreateTeam")
    public R createTeam(@RequestBody Team team){
        //进行插入
        //return teamService.insertTeam(team)?R.ok():R.error();
        return teamService.getBaseMapper().insert(team)>0?R.ok():R.error();
    }
    //删
    @PostMapping("/POST/DeleteTeam")
    public R deleteTeamByID(@RequestBody Team team){
        //现在project里面删除班组
        projectService.getBaseMapper().update(null, new UpdateWrapper<Project>().set("team_id", null).eq("team_id", team.getTeamId()));
        teamService.getBaseMapper().deleteById(team);
        //进行删除
        return R.ok();
    }
    //改
    @PostMapping("/POST/UpdateTeam")
    public R updateTeam(@RequestBody Team team){
        teamService.updateById(team);
        //进行更新
        return R.ok();
    }
    @PostMapping("/POST/getTeamInfo")
    public R getTeamInfo(@RequestParam("pageNum") int pageNum,
                         @RequestParam("pageSize") int pageSize){

        List<Team> teamList = teamService.getAllTeam();

        //进行分页查询
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, teamList.size());
        if (startIndex >= teamList.size()){
            return R.error();
        }
        // 使用subList获取指定范围的子列表，并创建一个新的ArrayList
        List<Team> pageData = new ArrayList<>(teamList.subList(startIndex, endIndex));


        return R.ok().data("TeamAttendances",new JSONArray(pageData))
                .data("TeamAttendancesSize",pageData.size());
    }

    @PostMapping("/GET/GetTeamInfoByName")
    public R getTeamInfoByName(@RequestParam("pageNum") int pageNum,
                               @RequestParam("pageSize") int pageSize,
                               @RequestParam(value="teamName") String teamName){
        List<Team> teamList = teamService.getTeamByName(teamName);

        //进行分页查询
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, teamList.size());
        if (startIndex >= teamList.size()){
            return R.error();
        }
        // 使用subList获取指定范围的子列表，并创建一个新的ArrayList
        List<Team> pageData = new ArrayList<>(teamList.subList(startIndex, endIndex));


        return R.ok().data("TeamAttendances",new JSONArray(pageData))
                .data("TeamAttendancesSize",pageData.size());
    }
    @PostMapping("/Get/GetTeamInfoByTeamSize")
    public R getTeamInfoByTeamSize(@RequestParam("pageNum") int pageNum,
                                   @RequestParam("pageSize") int pageSize,
                                   @RequestParam(value="teamSize") String teamSize){
        List<Team> teamList = teamService.getBaseMapper().selectList(new QueryWrapper<Team>().eq("team_size",teamSize));

        //进行分页查询
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, teamList.size());
        if (startIndex >= teamList.size()){
            return R.error();
        }
        // 使用subList获取指定范围的子列表，并创建一个新的ArrayList
        List<Team> pageData = new ArrayList<>(teamList.subList(startIndex, endIndex));


        return R.ok().data("TeamAttendances",new JSONArray(pageData))
                .data("TeamAttendancesSize",pageData.size());
    }
    @PostMapping("/GET/GetTeamInfoByContractor")
    public R getTeamInfoByContractor(@RequestParam("pageNum") int pageNum,
                                     @RequestParam("pageSize") int pageSize,
                                     @RequestParam(value="contractor") String contractor){

        List<Team> teamList = teamService.getBaseMapper().selectList(new QueryWrapper<Team>().like("Contractor",contractor));

        //进行分页查询
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, teamList.size());
        if (startIndex >= teamList.size()){
            return R.error();
        }
        // 使用subList获取指定范围的子列表，并创建一个新的ArrayList
        List<Team> pageData = new ArrayList<>(teamList.subList(startIndex, endIndex));


        return R.ok().data("TeamAttendances",new JSONArray(pageData))
                .data("TeamAttendancesSize",pageData.size());
    }



}
